package com.xcl.config;

import com.xcl.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    LoginInterceptor loginInterceptor;

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册登录认证拦截器，并且设置不拦截登录、注册请求
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/login","/register");
    }

    //跨域支持

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedHeaders("*").
                allowedMethods("GET", "POST", "PUT", "DELETE").
                allowedOrigins("http://localhost:5173","http://localhost:5174","http://127.0.0.1:5173").
                allowCredentials(true);
    }
}
